package com.toolmanrln.security.util;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @author ToolMan.RLN
 * @since 2020-05-25 16:47
 */
public class UserDetailsUtil {

    private UserDetailsUtil(){
        throw new UnsupportedOperationException("This class should not be instantiated.");
    }

    /**
     *  获取当前登录用户
     * @param
     * @return com.toolmanrln.logistics.modules.user.entity.User
     * @throws
     * @author ToolMan.RLN
     * @date 2020-05-25 10:16
     */
    public static UserDetails getUserDetails() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            if (authentication instanceof AnonymousAuthenticationToken) {
                return null;
            }
            if (authentication instanceof UsernamePasswordAuthenticationToken) {
                return (UserDetails) authentication.getPrincipal();
            }
        }
        return null;
    }
}
